Completed
Push — master ( 158ed8...34f194 )
by Alejandro
04:53 queued 02:19
created

CreateShortUrlResult.js ➔ componentDidMount   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
crap 1
1
import { faCopy as copyIcon } from '@fortawesome/free-regular-svg-icons';
2
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
3
import { isNil } from 'ramda';
4
import React from 'react';
5
import { CopyToClipboard } from 'react-copy-to-clipboard';
6
import { Card, CardBody, Tooltip } from 'reactstrap';
7
import PropTypes from 'prop-types';
8
import { createShortUrlResultType } from '../reducers/shortUrlCreation';
9
import './CreateShortUrlResult.scss';
10
11 4
const CreateShortUrlResult = (stateFlagTimeout) => class CreateShortUrlResult extends React.Component {
12
  static propTypes = {
13
    resetCreateShortUrl: PropTypes.func,
14
    error: PropTypes.bool,
15
    result: createShortUrlResultType,
16
  };
17
18
  state = { showCopyTooltip: false };
19
20
  componentDidMount() {
21 4
    this.props.resetCreateShortUrl();
22
  }
23
24
  render() {
25 4
    const { error, result } = this.props;
26
27 4
    if (error) {
28 1
      return (
29
        <Card body color="danger" inverse className="bg-danger mt-3">
30
          An error occurred while creating the URL :(
31
        </Card>
32
      );
33
    }
34 3
    if (isNil(result)) {
35 1
      return null;
36
    }
37
38 2
    const { shortUrl } = result;
39 2
    const onCopy = () => stateFlagTimeout(this.setState.bind(this), 'showCopyTooltip');
40
41 2
    return (
42
      <Card inverse className="bg-main mt-3">
43
        <CardBody>
44
          <b>Great!</b> The short URL is <b>{shortUrl}</b>
45
46
          <CopyToClipboard text={shortUrl} onCopy={onCopy}>
47
            <button
48
              className="btn btn-light btn-sm create-short-url-result__copy-btn"
49
              id="copyBtn"
50
              type="button"
51
            >
52
              <FontAwesomeIcon icon={copyIcon} /> Copy
53
            </button>
54
          </CopyToClipboard>
55
56
          <Tooltip placement="left" isOpen={this.state.showCopyTooltip} target="copyBtn">
57
            Copied!
58
          </Tooltip>
59
        </CardBody>
60
      </Card>
61
    );
62
  }
63
};
64
65
export default CreateShortUrlResult;
66